/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package testgui;


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.lang.Math;



/**
 *  Inspiration from: Java, Java, Java - Object-oriented Problem Solving
 *  Small amounts of code (mainly the code for the createImageIcon() method
 *  is taken from the icon tutoral by Sun at: 
 *  http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html
 * @author Christian Aastrup
 */
public class SimpleGUI extends JFrame implements KeyListener, ActionListener 
{
    private GridLayout layout = new GridLayout(1,1);
    private GridBagLayout gridBagLayout = new GridBagLayout();
    private JLabel hexGridJLabel = new JLabel();
    private JPanel hexGridJPanel;
    private JLabel playerJLabel = new JLabel();
    private Player player1;
    private Engine gridEngine;
    //private 
    
    
    private int[] playerStartingPos = new int[2];
    private int numberOfEnemies = 2;
    private Enemy[] enemyArray;
    private JLabel[] JLabelArrayForEnemies;
    private int[][] enemyStartingPositions;
    
    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JMenu helpMenu;
    private JMenuItem openJMenuItem;
    private JMenuItem exitJMenuItem;
    private JMenuItem aboutJMenuItem;

    public SimpleGUI()  
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        playerStartingPos[0] = 297;
        playerStartingPos[1] = 337;
        
        setSize(600,750);
        
        addKeyListener(this);
        hexGridJPanel = new JPanel();
        
        createEnemyStartingPositions();

        createJLabels();
      
        setJLabelPositionsAndSize();
        setEnemyJLabelsPositionAndSize();
        
        //System.out.println(playerJLabel);
        player1 = new Player(playerStartingPos, 18,25, 9,12, playerJLabel);
        
        createEnemies();

        gridEngine = new Engine(hexGridJLabel.getIcon().getIconWidth(),
                                hexGridJLabel.getIcon().getIconHeight());

        //changeLookAndFeel();
        Container containerPane = getContentPane();
       
        //Container containerForPlayer = getContentPane();
        //containerForPlayer.setLayout(gridBagLayout); 
        containerPane.setLayout(null);        
        containerPane.add(playerJLabel);
        containerPane.add(JLabelArrayForEnemies[0]);
        containerPane.add(JLabelArrayForEnemies[1]);
        containerPane.add(hexGridJLabel);
        
        
        createMenu();
        this.setJMenuBar(menuBar);
        
        
        setVisible(true);

    
    }
    
    private void createMenu()
    {
        menuBar = new JMenuBar();
        fileMenu = new JMenu("File");
        helpMenu = new JMenu("Help");
        
       openJMenuItem = new JMenu("Open");
       openJMenuItem.addActionListener(this);
       
       exitJMenuItem = new JMenu("Exit");
       exitJMenuItem.addActionListener(this);
       
       aboutJMenuItem = new JMenu("About");
       aboutJMenuItem.addActionListener(this);

       
       fileMenu.add(exitJMenuItem);
       helpMenu.add(aboutJMenuItem);
       
       menuBar.add(fileMenu);
       menuBar.add(helpMenu);
        
    }
    
    private void createActionListener()
    {
        
    }
    
    
    private void createEnemyStartingPositions()
    {
        int[] sPos = new int[2];
        enemyArray = new Enemy[numberOfEnemies];
        enemyStartingPositions = new int[2][numberOfEnemies];
        
        enemyStartingPositions[0][0] = 12;
        enemyStartingPositions[0][1] = 5;
        enemyStartingPositions[1][0] = 550;
        enemyStartingPositions[1][1] = 5;
    }

    private void createEnemies()
    {
        enemyArray[0] = new Enemy(enemyStartingPositions[0], 18,25, 0,0, 
                                  JLabelArrayForEnemies[0]);
 
        enemyArray[1] = new Enemy(enemyStartingPositions[1], 18,25, 17,0, 
                                  JLabelArrayForEnemies[1]);
 
    }
    
    private void setEnemyJLabelsPositionAndSize()
    {
        JLabelArrayForEnemies[0].setBounds(enemyStartingPositions[0][0],
                enemyStartingPositions[0][1],
                JLabelArrayForEnemies[0].getIcon().getIconWidth(),
                JLabelArrayForEnemies[0].getIcon().getIconHeight()); 
        JLabelArrayForEnemies[1].setBounds(enemyStartingPositions[1][0],
                enemyStartingPositions[1][1],
                JLabelArrayForEnemies[0].getIcon().getIconWidth(),
                JLabelArrayForEnemies[0].getIcon().getIconHeight());
    }
    
    private void setJLabelPositionsAndSize()
    {
        int widthOfhexGrid = hexGridJLabel.getIcon().getIconWidth();
        int heightOfhexGrid = hexGridJLabel.getIcon().getIconHeight();
        int horizontalMiddleOfhexGrid = (int) (widthOfhexGrid / 2);
        int verticalMiddleOfhexGrid = (int) (heightOfhexGrid / 2);
        
        hexGridJLabel.setBounds(0,0, widthOfhexGrid,heightOfhexGrid);
        playerJLabel.setBounds(playerStartingPos[0],playerStartingPos[1], 
                                playerJLabel.getIcon().getIconWidth(),
                                playerJLabel.getIcon().getIconHeight());
    }
    
    private void makeEnemiesMove()
    {
        for(int i = 0; i < numberOfEnemies; i++)
        {
            enemyArray[i].persuePlayer(player1, this);
        }
    }
    
    private void createJLabels()
    {
        createPlayerJLabel();
        createHexGridJLabel();
        createEnemyJLabels();
    }
    
    public Engine getEngineUsed()
    {
        return gridEngine;
    }
    
    
    
    private void movePlayer(int x, int y)
    {
        
    }
    private void addContentToJPanel()
    {
        hexGridJPanel.add(hexGridJLabel);
    }
    
    private void createEnemyJLabels()
    {
        JLabelArrayForEnemies = new JLabel[numberOfEnemies];
        System.out.println(numberOfEnemies);
        
        ImageIcon enemyImageIcon = createImageIcon("zombie_enemy.gif",
                                         "an enemy zombie");
        JLabelArrayForEnemies[0] = new JLabel(enemyImageIcon);
        JLabelArrayForEnemies[1] = new JLabel(enemyImageIcon);
    }
    
    private void createPlayerJLabel()
    {
        ImageIcon playerImageIcon = createImageIcon("zombie.gif",
                                         "a zombie");
        playerJLabel = new JLabel(playerImageIcon);
    }
    
    private void createHexGridJLabel()
    {
       ImageIcon hexGridImageIcon = createImageIcon("GridHex.png",
                                         "a hex grid");
        hexGridJLabel = new JLabel(hexGridImageIcon);
         
    }

    protected static ImageIcon createImageIcon(String path,
                                               String description) {
        java.net.URL imgURL = SimpleGUI.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
    
    public void changeLookAndFeel()
    {
        try
        {
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        }
        catch (Exception e) 
        {
            System.out.println("Error");
        }
   
    }
    
    public JLabel getPlayer1JLabel()
    {
        return playerJLabel;
    }
    
    public void changeJLabelPosition(JLabel jLabel, int x, int y)
    {
        jLabel.setBounds(x, y, 
                jLabel.getIcon().getIconWidth(),
                jLabel.getIcon().getIconHeight());
    }
    
  
    
public int getPlayer1JLabelXPosition()
{
    return (int) playerJLabel.getBounds().getX();
}

public int getPlayer1JLabelYPosition()
{
    return (int) playerJLabel.getBounds().getY();
}



public void keyTyped(KeyEvent e)
{
    
}   

public void keyPressed(KeyEvent e)
{
  int keyCodeOfKeyPressed = e.getKeyCode();
  
  switch(keyCodeOfKeyPressed)
  {
      case(KeyEvent.VK_NUMPAD7):
            player1.movePlayerNorthWest(this);
            makeEnemiesMove();
           break;
          
      case(KeyEvent.VK_NUMPAD9):
          player1.movePlayerNorthEast(this);
          makeEnemiesMove();
          break;
          
      case(KeyEvent.VK_NUMPAD4):
          player1.movePlayerWest(this);
          makeEnemiesMove();
          break;
          
      case(KeyEvent.VK_NUMPAD6):
          player1.movePlayerEast(this);
          makeEnemiesMove();
          break;
          
      case(KeyEvent.VK_NUMPAD1):
          player1.movePlayerSouthWest(this);
          makeEnemiesMove();
          break;
      
      case(KeyEvent.VK_NUMPAD3):
          player1.movePlayerSouthEast(this);
          makeEnemiesMove();
          break;
  }
  


}


public void keyReleased(KeyEvent e)
{
    
}

public void actionPerformed(ActionEvent e)
{
    JMenuItem jmi = (JMenuItem) e.getSource();
    System.out.println(jmi);
}
}


